package com.jy.horny.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jy.horny.R

/**
 * 年月日份
 */
class RegisterDateAdapter :
    RecyclerView.Adapter<RegisterDateAdapter.MonthViewHolder>() {
    private val dataList: MutableList<String> = mutableListOf()
    private var listener: OnItemClickListener? = null
    private var type: Int = 0;

    interface OnItemClickListener {
        fun onItemClick(position: Int, type: Int)
    }

    fun set(sourceList: List<String>?, type: Int) {
        this.type = type
        dataList.clear()
        sourceList?.let { dataList.addAll(it) }
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.listener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MonthViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_register_date, parent, false)
        return MonthViewHolder(itemView, listener, type)
    }

    override fun onBindViewHolder(holder: MonthViewHolder, position: Int) {
        val currentItem = dataList[position]
        holder.monthName.text = currentItem
    }

    override fun getItemCount() = dataList.size

    class MonthViewHolder(itemView: View, listener: OnItemClickListener?, type: Int) :
        RecyclerView.ViewHolder(itemView) {
        val monthName: TextView = itemView.findViewById(R.id.itemRegisterDate)

        init {
            itemView.setOnClickListener {
                listener?.onItemClick(adapterPosition,type)
            }
        }
    }
}